package com.stack.knowyouweb.controller;

import com.google.gson.Gson;

import com.stack.knowyoubase.constant.GlobalConst;
import com.stack.knowyoubean.bean.CourseAtlas;
import com.stack.knowyoubean.bean.RepJson;
import com.stack.knowyouservice.service.CourseAtlasService;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * 图谱服务层
 *
 * @author stack
 */
@RestController
@Scope("singleton")
@RequestMapping("/api")
public class CourseAtlasController {

    @Resource
    private CourseAtlasService courseAtlasService;
    @Resource
    private Gson gson;

    @GetMapping("/atlas")
    public String courseAtlasInfo(@RequestParam(value = "source", required = false) String source, HttpSession session) {
        Integer sid = (Integer) session.getAttribute("courseid");
        RepJson<CourseAtlas> repJson = new RepJson<>();
        repJson.setCode(GlobalConst.SUCCESS_CODE);
        if (sid == null) {
            repJson.setData(null);
            repJson.setMsg("没有选择课程");
            return gson.toJson(repJson);
        } else {
            CourseAtlas courseAtlas = courseAtlasService.courseAtlasSearch(source, sid);
            repJson.setData(courseAtlas);
            if (courseAtlas.getNodes().size() == 0) {
                repJson.setMsg("没有查找到相关节点");
            } else {
                repJson.setMsg("查询成功");
            }
            return gson.toJson(repJson);
        }
    }

    @PostMapping("/atlasNode")
    public String atlasNodeAdd(CourseAtlas.Node node, HttpSession session) {
        Integer sid = (Integer) session.getAttribute("courseid");
        if (sid == null) {
            RepJson<String> repJson = new RepJson<>();
            repJson.setCode(GlobalConst.SUCCESS_CODE);
            repJson.setData(GlobalConst.ERROR_TYPE);
            repJson.setMsg("没有选择课程");
            return gson.toJson(repJson);
        } else {
            node.setSid(sid);
            boolean b = courseAtlasService.atlasNodeAdd(node);
            if (b) {
                RepJson<String> repJson = new RepJson<>();
                repJson.setCode(GlobalConst.SUCCESS_CODE);
                repJson.setData(GlobalConst.SUCCESS_TYPE);
                repJson.setMsg("添加节点成功");
                return gson.toJson(repJson);
            } else {
                RepJson<String> repJson = new RepJson<>();
                repJson.setCode(GlobalConst.SUCCESS_CODE);
                repJson.setData(GlobalConst.ERROR_TYPE);
                repJson.setMsg("添加节点失败");
                return gson.toJson(repJson);
            }
        }
    }


    @PutMapping("/atlasNode")
    public String atlasNodeUpdate(CourseAtlas.Node node, HttpSession session) {
        Integer sid = (Integer) session.getAttribute("courseid");
        if (sid == null) {
            RepJson<String> repJson = new RepJson<>();
            repJson.setCode(GlobalConst.SUCCESS_CODE);
            repJson.setData(GlobalConst.ERROR_TYPE);
            repJson.setMsg("没有选择课程");
            return gson.toJson(repJson);
        } else {
            node.setSid(sid);
            boolean b = courseAtlasService.atlasNodeUpdate(node);
            if (b) {
                RepJson<String> repJson = new RepJson<>();
                repJson.setCode(GlobalConst.SUCCESS_CODE);
                repJson.setData(GlobalConst.SUCCESS_TYPE);
                repJson.setMsg("更新节点成功");
                return gson.toJson(repJson);
            } else {
                RepJson<String> repJson = new RepJson<>();
                repJson.setCode(GlobalConst.SUCCESS_CODE);
                repJson.setData(GlobalConst.ERROR_TYPE);
                repJson.setMsg("更新节点失败");
                return gson.toJson(repJson);
            }
        }
    }

    @DeleteMapping("/atlasNode")
    public String atlasNodeDelete(String name, HttpSession session) {
        Integer sid = (Integer) session.getAttribute("courseid");
        if (sid == null) {
            RepJson<String> repJson = new RepJson<>();
            repJson.setCode(GlobalConst.SUCCESS_CODE);
            repJson.setData(GlobalConst.ERROR_TYPE);
            repJson.setMsg("没有选择课程");
            return gson.toJson(repJson);
        } else {
            CourseAtlas.Node node = new CourseAtlas.Node();
            node.setSid(sid);
            node.setName(name);
            boolean b = courseAtlasService.atlasNodeDelete(node);
            if (b) {
                RepJson<String> repJson = new RepJson<>();
                repJson.setCode(GlobalConst.SUCCESS_CODE);
                repJson.setData(GlobalConst.SUCCESS_TYPE);
                repJson.setMsg("删除节点成功");
                return gson.toJson(repJson);
            } else {
                RepJson<String> repJson = new RepJson<>();
                repJson.setCode(GlobalConst.SUCCESS_CODE);
                repJson.setData(GlobalConst.ERROR_TYPE);
                repJson.setMsg("删除节点失败");
                return gson.toJson(repJson);
            }
        }
    }

    @PostMapping("/atlasNodeLink")
    public String createAtlasNodeLink(String preName, String afterName, String rela, HttpSession session) {
        Integer sid = (Integer) session.getAttribute("courseid");
        if (sid == null) {
            RepJson<String> repJson = new RepJson<>();
            repJson.setCode(GlobalConst.SUCCESS_CODE);
            repJson.setData(GlobalConst.ERROR_TYPE);
            repJson.setMsg("没有选择课程");
            return gson.toJson(repJson);
        } else {
            boolean b = courseAtlasService.atlasEdgeAdd(preName, afterName, rela, sid);
            if (b) {
                RepJson<String> repJson = new RepJson<>();
                repJson.setCode(GlobalConst.SUCCESS_CODE);
                repJson.setData(GlobalConst.SUCCESS_TYPE);
                repJson.setMsg("添加关系成功");
                return gson.toJson(repJson);
            } else {
                RepJson<String> repJson = new RepJson<>();
                repJson.setCode(GlobalConst.SUCCESS_CODE);
                repJson.setData(GlobalConst.ERROR_TYPE);
                repJson.setMsg("添加关系失败");
                return gson.toJson(repJson);
            }
        }
    }

    @PutMapping("/atlasNodeLink")
    public String updateAtlasNodeLink(String preName, String afterName, String rela, HttpSession session) {
        Integer sid = (Integer) session.getAttribute("courseid");
        if (sid == null) {
            RepJson<String> repJson = new RepJson<>();
            repJson.setCode(GlobalConst.SUCCESS_CODE);
            repJson.setData(GlobalConst.ERROR_TYPE);
            repJson.setMsg("没有选择课程");
            return gson.toJson(repJson);
        } else {
            boolean b = courseAtlasService.atlasEdgeUpdate(preName, afterName, rela, sid);
            if (b) {
                RepJson<String> repJson = new RepJson<>();
                repJson.setCode(GlobalConst.SUCCESS_CODE);
                repJson.setData(GlobalConst.SUCCESS_TYPE);
                repJson.setMsg("更新关系成功");
                return gson.toJson(repJson);
            } else {
                RepJson<String> repJson = new RepJson<>();
                repJson.setCode(GlobalConst.SUCCESS_CODE);
                repJson.setData(GlobalConst.ERROR_TYPE);
                repJson.setMsg("更新关系失败");
                return gson.toJson(repJson);
            }
        }
    }

    @DeleteMapping("/atlasNodeLink")
    public String deleteAtlasNodeLink(String preName, String afterName, HttpSession session) {
        Integer sid = (Integer) session.getAttribute("courseid");
        if (sid == null) {
            RepJson<String> repJson = new RepJson<>();
            repJson.setCode(GlobalConst.SUCCESS_CODE);
            repJson.setData(GlobalConst.ERROR_TYPE);
            repJson.setMsg("没有选择课程");
            return gson.toJson(repJson);
        } else {
            boolean b = courseAtlasService.atlasEdgeDelete(preName, afterName, sid);
            if (b) {
                RepJson<String> repJson = new RepJson<>();
                repJson.setCode(GlobalConst.SUCCESS_CODE);
                repJson.setData(GlobalConst.SUCCESS_TYPE);
                repJson.setMsg("删除关系成功");
                return gson.toJson(repJson);
            } else {
                RepJson<String> repJson = new RepJson<>();
                repJson.setCode(GlobalConst.SUCCESS_CODE);
                repJson.setData(GlobalConst.ERROR_TYPE);
                repJson.setMsg("删除关系失败");
                return gson.toJson(repJson);
            }
        }
    }

}
